<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class PreferencesPage {

	private $alerter;
	
	public function __construct() {
		$this->alerter = new Alerter();
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		check_access("VIEW");
	}

	function page_inc_execute_action($page, $action) {
		global $CONFIG;

		switch($action) {
			default:
				$this->alerter->add_alert("unknown action: $action");
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {

		if ($_POST['submitted']) {
			
			
			if (check_int($_POST['pref_numposts'], "aantal posts per pagina")) {
				if (!$_POST['skin'])
					errorpage(9);
				if ($_SESSION['user_id']) {
					$_SESSION['member']['pref_skin'] = $_POST['skin'];
					$_SESSION['member']['pref_numposts'] = $_POST['pref_numposts'];
					db_do_query($page, db_query_update(array('member_since', 'last_visit', 'pref_skin', 'pref_numposts'), 
								array('member_since', 'last_visit', db_sqlize($_POST['skin']), intval($_POST['pref_numposts'])), 
								'MEMBER', "user_id = {$_SESSION['user_id']}"));
				} else {
					$_SESSION['member']['skin'] = $_POST['skin'];
					$_SESSION['member']['pref_numposts'] = $_POST['pref_numposts'];
				}
				endpage("OK, je voorkeuren zijn aangepast");
			}
		}
		
		$inputs = array();
		
		$creator = new FormCreator();
		$inputs[] = $creator->header("Algemeen");
		
		$inputs[] = $creator->input_field("# reacties per pagina", "pref_numposts", $_SESSION['member']['pref_numposts']);
		
		$kv = get_keys_and_values("name", "description", "SKIN");
		global $CONFIG;
		$old = $_SESSION['user_id'] ? $_SESSION['member']['pref_skin'] : $_SESSION['skin'];
		$old = $old ? $old : $CONFIG['default_skin'];
		$keys = array();
		$values = array();
		while ($k = array_pop($kv['keys'])) {
			$v = array_pop($kv['values']);
			$keys[] = $k;
			$values[] = "$k - $v";
		}
		$inputs[] = $creator->input_select("Kies je skin", "skin", $keys, $values, '', $old);
		if (!$_SESSION['user_id'])
			$inputs[] = $creator->info("De gegevens die je hier aanpast blijven maar geldig gedurende deze sessie. Je kan <a href=/service/.register?action=add>registreren</a> en <a href=.login>inloggen</a> om je instellingen te bewaren.");
		$page->content['form'] = $creator->create("prefform", array_reverse($inputs));

		$page->content['title'] = "Voorkeuren" . ($_SESSION['user_id'] ? ": {$_SESSION['member']['nick']}" : "");
	}

	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		print_body($page);
		print_form($page,  $page->content['form']);
	}
}

?>
